//
//  Cell.m
//  自动计算单元格高度
//
//  Created by vera on 14-9-13.
//  Copyright (c) 2014年 vera. All rights reserved.
//

#import "Cell5_1.h"
#import "GBRequest.h"

#define docid @"&docId="
#define collectUrl @"method=add&token="
#define cancleCollectUrl @"method=deleteFav&token="
@interface Cell5_1 ()
//被选中的需要被删除的那一行的tag
@property (nonatomic, assign) NSInteger selectTag;

@end
@implementation Cell5_1

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = WQColor(241, 243, 244);
        
        UIView *container_ = [[UIView alloc] initWithFrame:CGRectMake(0, 4,screenW, 82)];
        //container_.layer.cornerRadius = 3.0;
        container_.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:container_];
        // Initialization code
        _titleLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(12, -10,screenW - 170, 50)];
        _titleLabel1.numberOfLines = 0;
        _titleLabel1.font = [UIFont systemFontOfSize:15];
        _titleLabel1.text = @"深圳";
        [container_ addSubview:_titleLabel1];
        
        _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, 48, 80, 30)];
        _timeLabel.numberOfLines = 0;
        _timeLabel.text = @"2015-05-03";
        _timeLabel.font = [UIFont systemFontOfSize:10.5];
        _timeLabel.textColor = [UIColor lightGrayColor];
        [container_ addSubview:_timeLabel];
        
        _readBtn = [UIButton buttonWithType:0];
        _readBtn.frame = CGRectMake(screenW - 40, 57, 25, 25);
        _readBtn.titleLabel.font = [UIFont systemFontOfSize:10];
        [_readBtn setTitle:@"删除" forState:UIControlStateNormal];
        [_readBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [_readBtn addTarget:self action:@selector(shanchu:) forControlEvents:UIControlEventTouchUpInside];
        //[container_ addSubview:_readBtn];
        
        _deleteImage = [[UIImageView alloc] initWithFrame:CGRectMake(screenW - 55, 39, 15, 15)];
        //NSLog(@"%f",self.contentView.frame.size.width);
        _deleteImage.image = [UIImage imageNamed:@"删除"];
        //[container_ addSubview:_deleteImage];
    }
    return self;
}

#pragma mark - 删除按钮的操作
-(void)shanchu:(UIButton *)sender
{
    KDAccount *account = [KDAccountTool account];
    
    //NSLog(@"%ld",(long)sender.tag);
    NSString *sssss = [NSString stringWithFormat:@"%ld%@",(long)sender.tag,@"Dele"];
    //NSUserDefaults
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    //读取数据
    NSString *str = [userDefaults stringForKey:sssss];
    
    //取消收藏
    _deleteUrl = [NSString stringWithFormat:@"%@&%@%@%@%@",ServerActionAddr,cancleCollectUrl,account.tokens,docid,str];
    KDLog(@"%@", _deleteUrl);
    self.selectTag = sender.tag;
    [self requestData];
}

#pragma mark - 请求数据
-(void)requestData
{
    GBRequest *request1 = [[GBRequest alloc]initWithUrlStr:_deleteUrl];
    request1.tag = 1;
    request1.delegate = self;
    [request1 startRequestData];
}

#pragma mark - 数据成功请求完毕
- (void)requestDidFinished:(GBRequest *)request
{
    NSString *str = [[NSString alloc] initWithData:request.requestData encoding:NSUTF8StringEncoding];
    //解析
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    id dataArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSDictionary *jsonDict = nil;
    //NSLog(@"%@",jsonDict);
    if ([dataArray isKindOfClass:[NSDictionary class]])
    {
        jsonDict = dataArray;
        //NSLog(@"%@",dict[@"status"]);
    }
    if([jsonDict[@"status"] isEqualToString:@"ok"])
    {
//        NSLog(@"%@",_deleteUrl);
        NSString *kkkkk = [NSString stringWithFormat:@"%ld",self.selectTag];
        NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:kkkkk,@"deletetS", nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"deleShow" object:nil userInfo:userInfo];

    }
}
#pragma mark - 数据请求失败
- (void)requestDidFailedWithError:(NSError *)error
{
    UIAlertView *messageView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"删除失败！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
    [messageView show];
    

}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
